package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.MemberInfo;
import cn.wolfcode.crm.qo.MemberInfoQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.service.IMemberInfoService;
import cn.wolfcode.crm.util.JSONResult;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.RichTextString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("memberInfo")
public class MemberInfoController {
    @Autowired
    private IMemberInfoService memberInfoService;




    @RequestMapping("view")
    public String list(){
        return "/memberInfo/list";
    }
   @RequestMapping("analysis")
    public String analysis(){
        return "/memberInfo/analysis";
    }

    //异步加载数据
    @RequestMapping("query")
    @ResponseBody
    public Object list(@ModelAttribute("qo") MemberInfoQueryObject qo, Model model) {
        //将数据转换成json格式
        PageResult result = memberInfoService.queryForList(qo);

        return result;
    }
    //异步加载数据
    //会员分析
    @RequestMapping("selectMemberConsumeInfo")
    @ResponseBody
    public Object selectMemberConsumeInfo(@ModelAttribute("qo") MemberInfoQueryObject qo) {
        //将数据转换成json格式
        PageResult result = memberInfoService.selectMemberConsumeInfo(qo);

        return result;
    }
    //会员分析
    @RequestMapping("selectData")
    @ResponseBody
    public Object selectData() {
        //将数据转换成json格式
        Map<String,Object> result = memberInfoService.selectData();

        return result;
    }

    // /异步加载数据
    @RequestMapping("queryByCardNumber")
    @ResponseBody
    public Object queryByCardNumber(String cardNumber) {
        //将数据转换成json格式
       MemberInfo info = memberInfoService.queryByCardNumber(cardNumber);
        return info;
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(MemberInfo entity){
        JSONResult result = new JSONResult();
        try {
            memberInfoService.saveOrUpdate(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("changeState")
    public Object changeState(Long id){
        memberInfoService.changeState(id);
        JSONResult json = new JSONResult();
        return json;
    }

    //数据导出

    @RequestMapping("exportData")
    public void exportData(HttpServletResponse response) throws IOException{

        //1.设置下载的响应头信息
        response.addHeader("Content-disposition","attachment:filename-memberInfo.xls");
        // 2.查询所有的用户信息
        List<MemberInfo> memberInfoList = memberInfoService.selectAll();
        //3.创建一个excel工作表
        HSSFWorkbook workbook = new HSSFWorkbook();
        //4.创建一个工作页签sheet
        HSSFSheet sheet = workbook.createSheet();
        HSSFRow header = sheet.createRow(0);
        //5.创建单元格信息
        header.createCell(0).setCellValue("会员姓名");
        header.createCell(1).setCellValue("卡号");
        header.createCell(2).setCellValue("电话号码");
        header.createCell(3).setCellValue("会员等级");
        header.createCell(4).setCellValue("可用积分");
        header.createCell(5).setCellValue("存储余额");
        header.createCell(6).setCellValue("性别");
        header.createCell(7).setCellValue("地址");
        header.createCell(8).setCellValue("消费金额");
        header.createCell(9).setCellValue("开卡时间");
        for (int i = 0; i < memberInfoList.size(); i ++){
            MemberInfo memberInfo = memberInfoList.get(i);
            HSSFRow currentRow = sheet.createRow(i + 1);
            //创建单元格信息
            currentRow.createCell(0).setCellValue(memberInfo.getName());
            currentRow.createCell(1).setCellValue(memberInfo.getCardnumber());
            currentRow.createCell(2).setCellValue(memberInfo.getTel());
            currentRow.createCell(3).setCellValue(memberInfo.getRankId());
            currentRow.createCell(4).setCellValue((RichTextString) memberInfo.getPoints());
            currentRow.createCell(5).setCellValue((RichTextString) memberInfo.getBalance());
            currentRow.createCell(6).setCellValue(memberInfo.isGender());
            currentRow.createCell(7).setCellValue(memberInfo.getAddress());
            currentRow.createCell(8).setCellValue((RichTextString) memberInfo.getConsumption());
            currentRow.createCell(9).setCellValue(memberInfo.getDate());
        }
        workbook.write(response.getOutputStream());
        //关闭流操作
        workbook.close();

    }

}
